package com.huawei.m2m.cig.decoder.protocol;

import java.nio.BufferOverflowException;

public abstract class OutputBuffer {
	protected byte[] buf = new byte[512];
	protected int pos = 0;

	protected void checkLength(int paramInt) {
		if (this.pos + paramInt > this.buf.length) {
			throw new BufferOverflowException();
		}
	}

	public void putByte(int paramInt) {
		checkLength(1);
		this.buf[(this.pos++)] = ((byte) paramInt);
	}

	public abstract void putInt16(int paramInt);

	public abstract void putInt24(int paramInt);

	public abstract void putInt32(int paramInt);

	public void putByteArray(byte[] paramArrayOfByte) {
		if (paramArrayOfByte == null) {
			return;
		}
		putByteArray(paramArrayOfByte, 0, paramArrayOfByte.length);
	}

	public void putByteArray(byte[] paramArrayOfByte, int paramInt1, int paramInt2) {
		if ((paramArrayOfByte == null) || (paramInt2 == 0)) {
			return;
		}
		checkLength(paramInt2);
		System.arraycopy(paramArrayOfByte, paramInt1, this.buf, this.pos, paramInt2);
		this.pos += paramInt2;
	}

	public byte[] toByteArray() {
		byte[] arrayOfByte = new byte[this.pos];
		System.arraycopy(this.buf, 0, arrayOfByte, 0, this.pos);
		return arrayOfByte;
	}

	public void putString(String paramString1, String paramString2) {
		byte[] arrayOfByte = null;
		arrayOfByte = hexStrToByteArray(paramString1);
		putByteArray(arrayOfByte);
	}

	private byte[] hexStrToByteArray(String str) {
		if (str == null) {
			return null;
		}
		if (str.length() == 0) {
			return new byte[0];
		}
		byte[] byteArray = new byte[str.length() / 2];
		for (int i = 0; i < byteArray.length; i++) {
			String subStr = str.substring(2 * i, 2 * i + 2);
			byteArray[i] = ((byte) Integer.parseInt(subStr, 16));
		}
		return byteArray;
	}
}
